/**
 * @Filename: testcase.h
 * @Description:
 * @Author: chengxiaojun
 * @Email: csforgood@gmail.com
 * @Time: 2011/11/11
 */
#ifndef __TESTCASE_H__

#define  __TESTCASE_H__

#include<string>
#include<vector>
#include<map>
#include<billboard.h>

using namespace std;


enum language_t{
    C,
    SHELL,
};

typedef struct CommonOperationST {

    /* outer */
    string opName;
    string opType;
    int    opExpect;    
    bool   opBlock; 
    int    opWait;
    int    opNum;

    int    opRet;

    /* language */
    language_t    opLanguage;
    
    /* inner */
    bool   opCheck;
    BillBoard *board;
    int opID;

    /* customized controls and attributes */ 
    map<string,string> opCtrls;
    map<string,string> opArgs;

    CommonOperationST () {
         opCheck = false;
         opBlock = true;
         opWait  = 0;
         opNum   = 1;
         board   = NULL; 
         opLanguage = C;
         opRet   = 0;
    }
    template<typename T>
    T lookupArgument(const string &attrName, T defaultValue) {
	    T attrValue;
	    map<string, string>::iterator it;
	    it = opArgs.find(attrName);
	    if (it != opArgs.end()) {
		    if(typeid(T) == typeid(int)) {
			    int* temp = (int*)(&attrValue);
			    *temp = atoi(it->second.c_str());
		    }
		    else if(typeid(T) == typeid(string))  {
			    string *temp = (string*)(&attrValue);
			    *temp = it->second;
		    }
	    }
	    else {
		    attrValue = defaultValue;
	    }
	    return attrValue;

    }
    template<typename T>
    T lookupAttribute(const string &attrName, T defaultValue) {
	    T attrValue;
	    map<string, string>::iterator it;
	    it = opCtrls.find(attrName);
	    if (it != opCtrls.end()) {
		    if(typeid(T) == typeid(int)) {
			    int* temp = (int*)(&attrValue);
			    *temp = atoi(it->second.c_str());
                    }
		    else if(typeid(T) == typeid(string))  {
			    string *temp = (string*)(&attrValue);
			    *temp = it->second;
		    }
	    }
	    else {
		    attrValue = defaultValue;
	    }
	    return attrValue;

    }
}CommonOperationST;

typedef struct CommonCaseST{
	/* case name */
	string caseName;
	string caseDesc;
	/* case operations */
	vector<CommonOperationST> ops;
	/* billboad belonging to this case */
	BillBoard *board;
	string casePath;
	int caseRet;

	CommonCaseST():caseRet(1) {}
	/* setup/teardown */
}CommonCaseST;

typedef void    (*global_func)(void);
typedef int   (*operation_func)(CommonOperationST*);

/* functions */
template<typename T>
int getValueFromOperation(map<string,string> &opCtrls, const string &attrName, T &attrValue)
{
	map<string, string>::iterator it;
	it = opCtrls.find(attrName);
	if (it != opCtrls.end()) {
		if(typeid(T) == typeid(int))
			attrValue = atoi(it->second.c_str());
		else if(typeid(T) == typeid(string))  {
			string *temp = (string*)(&attrValue);
			*temp = it->second;
		}
		return 1;
	}
	else {
		return 0;
	} 

}
#endif
